package com.LFL.ajyx;

import jakarta.servlet.ServletException;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.util.Calendar;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;


public class Main extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");


        PrintWriter pw = response.getWriter();

        //回复给客户端一个信息
        pw.println("ok!");
        //利用request对象返回客户端来的输入流
        try (ServletInputStream sis = request.getInputStream()) {

            Calendar Time = null;
            OutputStream os = new FileOutputStream("C:\\Users\\25933\\Documents\\qianzhizhuapai\\"+Time.getTime()+".jpg");
            BufferedOutputStream bos = new BufferedOutputStream(os);

            byte[] buf= new byte[1024];
            int length = 0;
            length = sis.readLine(buf, 0, buf.length);//使用sis的读取数据的方法
            while(length!=-1) {
                bos.write(buf, 0, length);
                length = sis.read(buf);
            }
            sis.close();
            bos.close();
            os.close();
        }

    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //转到doGet中做处理
        doGet(request, response);
    }

}
